import { Component } from '@angular/core';
import { NavController, NavParams, ActionSheetController, AlertController, ToastController } from 'ionic-angular';

import { StorageService } from '../../providers/StorageService';
import { HomeService } from '../../providers/HomeService';
import { BuildService } from '../../providers/BuildService';
import { RoomService } from '../../providers/RoomService';

import { LoginPage } from "../login/login";
import { BuildPage } from "../build/build";
import { RoomPage } from "../room/room";
import { RoomDetailPage } from "../roomDetail/roomDetail";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers: [HomeService, BuildService, RoomService]
})
export class HomePage {
  user_info: any;
  user_id: number;
  rooms: any;
  builds: any;
  homeList: any;
  roomPage = 1;
  buildPage = 1;

  constructor(
    public navCtrl: NavController,
    public actionSheetCtrl: ActionSheetController,
    public alertCtrl: AlertController,
    private storageService: StorageService,
    private homeService: HomeService,
    private buildService: BuildService,
    private roomService: RoomService,
    private toastCtrl: ToastController,
    navParams: NavParams
  ) {
    this.user_info = this.storageService.read('UserInfo');
    this.user_id = this.user_info.user_id;

    this.homeService.getRoomList({'user_id': this.user_id, 'page': this.roomPage}).then(data => {
      this.rooms = data.room;
      this.roomPage++;
    });

    this.homeService.getBuildList({'user_id': this.user_id, 'page': this.buildPage}).then(data => {
      this.builds = data.build;
      this.buildPage++;
    });

    this.homeList = typeof navParams.get('listSelect') === 'undefined' ? 'roomList' : navParams.get('listSelect');
  }

  // 弹出选择框
  presentActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      title: '提示',
      buttons: [
        {
          text: '创建新地址',
          role: 'build',
          handler: () => {
            this.navCtrl.push(BuildPage, { item: 1 });
          }
        },
        {
          text: '创建新房间',
          role: 'room',
          handler: () => {
            this.navCtrl.push(RoomPage, { item: 1 });
          }
        }
      ]
    });
    actionSheet.present();
  }

  logout() {
    let actionSheet = this.actionSheetCtrl.create({
      title: '是否退出登录',
      buttons: [
        {
          text: '退出登录',
          role: 'build',
          handler: () => {
            this.navCtrl.push(LoginPage);
          }
        },
      ]
    });
    actionSheet.present();
  }

  // 查看事件
  registerViewRoom(room) {
    this.navCtrl.push(RoomDetailPage, { room_name: room.room_name, room_id: room.room_id });
  };

  //编辑事件
  registerEditRoom(room) {
    this.navCtrl.push(RoomPage, { room_id: room.room_id });
  };

  // 删除事件
  registerRemoveRoom(room) {
    let confirm = this.alertCtrl.create({
      title: '删除提示',
      message: '是否删除该房间?',
      buttons: [
        {
          text: '取消',
          handler: () => {}
        },
        {
          text: '确认',
          handler: () => {
            for(let i = 0; i < this.rooms.length; i++) {
              if (this.rooms[i] == room) {
                this.roomService.remove(room).then(data => {
                  if (data.status == 200)
                  {
                    this.rooms.splice(i, 1);
                    let toast = this.toastCtrl.create({
                      message: '删除成功.',
                      duration: 3000,
                      position: 'top',
                      showCloseButton: true,
                      closeButtonText: '关闭'
                    });
                    toast.present();
                  }
                  else {
                    let toast = this.toastCtrl.create({
                      message: '删除失败.',
                      duration: 3000,
                      position: 'top',
                      showCloseButton: true,
                      closeButtonText: '关闭'
                    });
                    toast.present();
                  }
                });
              }
            }
          }
        }
      ]
    });
    confirm.present();
  }

  registerEditBuild(build) {
    this.navCtrl.push(BuildPage, { build_id: build.build_id });
  }

  registerRemoveBuild(build) {
    let confirm = this.alertCtrl.create({
      title: '删除提示',
      message: '是否删除该地址?',
      buttons: [
        {
          text: '取消',
          handler: () => {}
        },
        {
          text: '确认',
          handler: () => {
            for(let i = 0; i < this.builds.length; i++) {
              if (this.builds[i] == build) {
                this.buildService.remove(build).then(data => {
                  if (data.status == 200)
                  {
                    this.builds.splice(i, 1);
                    let toast = this.toastCtrl.create({
                      message: '删除成功.',
                      duration: 3000,
                      position: 'top',
                      showCloseButton: true,
                      closeButtonText: '关闭'
                    });
                    toast.present();
                  }
                  else {
                    let toast = this.toastCtrl.create({
                      message: '删除失败.',
                      duration: 3000,
                      position: 'top',
                      showCloseButton: true,
                      closeButtonText: '关闭'
                    });
                    toast.present();
                  }
                });
              }
            }
          }
        }
      ]
    });
    confirm.present();
  }

  // 选择列表事件
  registerChangeList(list) {
    this.homeList = list;
  }

  doInfinite(infiniteScroll) {
    setTimeout(() => {
      if (this.homeList == 'roomList') {
        this.homeService.getRoomList({'user_id': this.user_id, 'page': this.roomPage}).then(data => {
          if (data.room.length > 0) {
            this.rooms = this.rooms.concat(data.room);
            this.roomPage++;
          }
        });
      } else {
        this.homeService.getBuildList({'user_id': this.user_id, 'page': this.buildPage}).then(data => {
          if (data.build.length > 0) {
            this.builds = this.builds.concat(data.build);
            this.buildPage++;
          }
        });
      }
      infiniteScroll.complete();
    }, 500);
  }
}

